package server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

import model.Message;

class MessageReceiverThread extends Thread
{
	private ObjectInputStream objectInputStream;
	private Client client;
	
	public MessageReceiverThread(final Socket socket, final Client client) throws IOException
	{
		this.objectInputStream = new ObjectInputStream(socket.getInputStream());
		this.client = client;
	}
	
	@Override
	public void run()
	{
		while (true) {
			try
			{
				Message message = (Message) objectInputStream.readObject();
				message.setFrom(this.client.getClientId());
				ServerMain.dispatcher.addMessage(message);
			}
			catch (IOException e)
			{
				ServerMain.removeClient(this.client);
				return;
			}
			catch (ClassNotFoundException e)
			{
				e.printStackTrace();
				return;
			}
		}
	}
	
}
